/**
 * OSS对象存储处理
 * */
layui.define(['sdHttp', 'sdUtils', 'globalConfig'], function (exports) {
    let sdHttp = layui.sdHttp;
    let sdUtils = layui.sdUtils;
    let globalConfig = layui.globalConfig;

    // 动态加载js
    let script = new sdUtils.script();
    script.set('/assets/libs/aliyun-oss-sdk.min.js')
    
    let oss = {
        put: function(fun){
            let param = {
                url: '/v1/tps/sts',
                callback: function (d) {
                    let client = new OSS.Wrapper({
                        region: d['Region'],
                        secure: true,
                        accessKeyId: d['AccessKeyID'],
                        accessKeySecret: d['AccessKeySecret'],
                        stsToken: d['SecurityToken'],
                        bucket: d['Bucket']
                    });
                    fun ? fun(client) : '';
                }
            }
            sdHttp.get(param);
        },
        upload: function (id, type, path) {
            let fileId = (type == 8 ? '#input-' + id : '#input-file-' + id);
            let view = (type == 8 ? '#layui-upload-' + id + '-list' : '#layui-upload-file-' + id + '-list');
            let files = (type == 8 ? document.getElementById("input-"+id).files : document.getElementById("input-file-"+id).files);
            // // 判断上传限制
            // // 当前上传限制量
            // let len = $(fileId).attr('data-len');
            // // 获取当前已经上传的数量
            // let nowLen = $(view).find('.file-item').size();
            // if(len == nowLen){
            //     alert("当前上传数量已到达上限");
            //     return false;
            // }
            // 准备上传的数量
            // let readyLen = files.length;
            // if((readyLen+nowLen) > len){
            //     alert("当前上传数量限制["+len+"]个文件");
            //     return false;
            // }

            let dir = path ? path : 'file/tem/';
            oss.put(function (client) {
                $.each(files, function(i, file){
                    let f = sdUtils.file.createFileName(file);
                    let temId = sdUtils.string.guid();
                    let obj = $('.common-form-view').find(view);
                    obj.append(upload.loading(temId));

                    client.multipartUpload(dir + f.name, file).then(function (result) {
                        let temDivId = '#div-' + temId;
                        if(type == 8){
                            if(f.type == 'video'){
                                upload.video(temDivId, result.name);
                            }else if(f.type == 'image'){
                                let tem = '#' + temId;
                                $('.common-form-view').find(tem).attr('src', upload.set(result.name, true));
                                $('.common-form-view').find(tem).attr('data-path', result.name);
                            }
                        } else{
                            upload.file(temDivId, result.name);
                        }
                    }).catch(function (err) {
                        console.log(err);
                    });
                })
                $(fileId).val('');
            })
        },
        set: function (id, paths, flag) {
            let view = '#layui-upload-' + id + '-list';
            $.each(paths, function (i, item) {
                let temId = sdUtils.string.guid();
                let obj = $('.common-form-view').find(view);
                obj.append(upload.loading(temId));
                if(item.indexOf('mp4') >= 0){
                    let temDivId = '#div-' + temId;
                    upload.video(temDivId, item);
                }else{
                    let tem = '#' + temId;
                    $('.common-form-view').find(tem).attr('src', upload.set(item, flag));
                    let img = item.replace(globalConfig.resource, '');
                    $('.common-form-view').find(tem).attr('data-path', img);
                }
            })
        }
    }

    let upload = {
        loading: function (id) {
            let html = '<div class="img-min" id="div-' + id + '">\n' +
                '           <img class="file-item" id="' + id + '" src="/assets/images/loading.gif">' +
                '           <span class="img-min-remove">X</span>'+
                '       </div>';
            return html;
        },
        set: function (path, flag) {
            if(flag){
                return globalConfig.resource + path + '?x-oss-process=style/mini-photo';
            }else{
                return path + '?x-oss-process=style/mini-photo';
            }
        },
        video: function (id, path) {
            $(id).find('img').remove();
            if(path.indexOf(globalConfig.resource) >= 0){
                let video = path.replace(globalConfig.resource, '');
                $(id).append('<video data-path="' + video + '" class="file-item" src="' + path + '" controls="controls"></video>');
            }else{
                let full = globalConfig.resource + path;
                $(id).append('<video data-path="' + path + '" class="file-item" src="' + full + '" controls="controls"></video>');
            }
        },
        file: function (id, path) {
            $(id).find('img').remove();
            if(path.indexOf(globalConfig.resource) >= 0){
                let video = path.replace(globalConfig.resource, '');
                $(id).append('<span data-path="' + video + '" class="file-item file-download" data-url="' + path + '">' + path + '</span>');
            }else{
                let full = globalConfig.resource + path;
                $(id).append('<span data-path="' + path + '" class="file-item file-download" data-url="' + full + '">' + path + '</span>');
            }
        }
    }


    exports('oss', oss);
})